package com.pocket.insight.base.mql.relation;

import com.pocket.insight.base.mql.Relation;
import com.pocket.insight.base.mql.TypedVal;
import com.pocket.insight.base.sql.Dialect;
import com.pocket.insight.base.sql.SchemaProvider;
import com.pocket.insight.base.sql.SqlEval;
import com.pocket.insight.entity.ME;

/**
 * 描述：
 *
 * @author shuize
 * @create 2024/12/8
 */
public class PrimeRelation extends BaseRelation {

    public ME             dataset;
    public SchemaProvider schemaProvider;

    public static Relation create(Dialect dialect, ME dataset) {
        PrimeRelation relation = new PrimeRelation();
        relation.dataset = dataset;
        relation.dialect = dialect;
        return relation;
    }

    public static Relation create(Dialect dialect, ME dataset, SchemaProvider schemaProvider) {
        PrimeRelation relation = new PrimeRelation();
        relation.dataset = dataset;
        relation.dialect = dialect;
        relation.schemaProvider = schemaProvider;
        return relation;
    }

    public TypedVal toQuery() {
        if ((fetchRunTime()).query == null) {
            if (this.dataset.op != null && this.dataset.op instanceof Relation) {
                Relation opRelation = (Relation) this.dataset.op;
                if (opRelation.getIsSourceVirtualDialect(getDialect()).booleanValue()) {
                    opRelation.setDialect(getDialect());
                }
            }
            SqlEval eval = SqlEval.createEval(this.dialect);
            (fetchRunTime()).query = eval.evaluate(this.dataset);
        }
        return (fetchRunTime()).query;
    }


    public void setSchemaProvider(SchemaProvider schemaProvider) {
        this.schemaProvider = schemaProvider;
    }

}
